package com.zzyl.nursing.service.impl;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * contractService业务层处理
 * 
 * @author lhs
 * @date 2025-05-23
 */
@Service
@Slf4j
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询contract
     * 
     * @param id contract主键
     * @return contract
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询contract列表
     * 
     * @param contract contract
     * @return contract
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增contract
     * 
     * @param contract contract
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return save(contract) ? 1 : 0;
    }

    /**
     * 修改contract
     * 
     * @param contract contract
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return updateById(contract) ? 1 : 0;
    }

    /**
     * 批量删除contract
     * 
     * @param ids 需要删除的contract主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除contract信息
     * 
     * @param id contract主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContractStatus() {
        //先获取数据库中所有合同的状态
        List<Contract> list = lambdaQuery().in(Contract::getStatus, 0, 1).list();
        log.info("获取数据库中未生效和已生效的合同:{}", list);
        //以生效和未生效为例子来判断
        LocalDateTime now =  LocalDateTime.now();
        List<Contract> updateList = new ArrayList<>();
        for (Contract contract : list) {
            //contract.getStatus() == 0,未生效
            if (contract.getStatus() == 0) {
                //如果开始时间小于系统时间说明已经生效
                if (contract.getStartDate().isBefore(now)){
                    contract.setStatus(1);
                }
                //如果结束时间大于小于系统时间说明已经过期
                if (contract.getEndDate().isBefore(now)){

                    contract.setStatus(3);
                }
                updateList.add(contract);
            }
            if (contract.getStatus() == 1) {
                //如果结束时间大于小于系统时间说明已经过期
                if (contract.getEndDate().isBefore(now)){
                    //设置状态为已过期
                    contract.setStatus(2);
                }
                updateList.add(contract);
            }
            updateBatchById(updateList);
        }
    }
}
